package org.dizena.common.trace.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

@Order(2)
@Configuration
@EnableConfigurationProperties(TraceFilterAutoProperties.class)
@ConditionalOnProperty(
        prefix = "common.trace.filter",
        value = {"enabled"},
        havingValue = "true",
        matchIfMissing = true
)
public class TraceFilterAutoConfig implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(TraceFilterAutoConfig.class);

    @Value("${spring.application.name}")
    private String service;

    @Bean
    public FilterRegistrationBean traceFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new TraceFilterImpl(service));
        registration.addUrlPatterns("/*");
        registration.setName("traceFilter");
        registration.setOrder(1);
        return registration;
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("【自动化配置】---Filter Trace 配置初始化");
    }
}